package com.example.demo.test.alc;

import com.example.demo.socket.WebSocketServer;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.stereotype.Component;

import java.util.Random;

import static com.example.demo.test.selenium.SeleniumHelper.*;
@Component
public class MyAddressBook implements BaseTest{

    public void test() throws Exception {
        WebSocketServer.screenInfoMsg("======================myAddressTest======================");

        WebSocketServer.screenInfoMsg("打开 MyAddressList页面");
        waitAndClickByPath("//a[@href='/us/en/addressbook']");

        WebSocketServer.screenInfoMsg("点击 newAddress ");
        waitAndClickByPath("//*[@id='shippingAddressContent']/div[2]/div/div[4]/button");

        WebSocketServer.screenInfoMsg("等待 AddressEdit 页面加载完成");
        waitByPath("//*[@id='shippingAddressContent']/div[1]");

        submitAddressBook();
    }

    public void submitAddressBook() {
        WebSocketServer.screenInfoMsg("生成 Address表单");
        int i = new Random().nextInt();
        String lastName = "xianwen" + i;

        findByIdAndClearSend("addrss_FirstName", "zhang");
        findByIdAndClearSend("addrss_LastName", lastName);
        findByIdAndClearSend("addrss_StreetName", "gx");
        findByIdAndClearSend("addrss_StreetNumber", "bgyxh");
        findByIdAndClearSend("addrss_Town", "zhengzhou");
        findByIdAndClearSend("addrss_PostalCode", "10000");
        findByIdAndClearSend("addrss_Company", "B1");
        findByIdAndClearSend("addrss_Phone1", "18888888888");
        findByIdAndClearSend("addrss_ExtPhone1", "188");
        findByIdAndClearSend("addrss_Phone2", "10000000000");
        findByIdAndClearSend("addrss_ExtPhone2", "199");
        findByIdAndClearSend("addrss_Email", "978880162@qq.com");

        WebSocketServer.screenInfoMsg("准备提交 Address表单");
        driver.findElement(By.xpath("//*[@id='address']/div[3]/button[2]")).click();
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(lastName)));
            WebSocketServer.screenInfoMsg("Address 保存成功");
        } catch (Exception e) {
            WebSocketServer.screenInfoMsg("Address 保存失败 lastName 与预期不一致 期望:%s 实际:%s", lastName, "未找到");
        }
    }

}
